<template>
  <div class="login">
    <el-form ref="form" :model="form" class="login-form">
      <h3 class="title">后台管理系统</h3>
      <el-form-item prop="ehuPhone">
        <el-input
          v-model="form.ehuPhone"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="ehuPwd">
        <el-input
          v-model="form.ehuPwd"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2019 xyshop.vip All Rights Reserved.</span>
    </div>
  </div>
</template>
<script>
import { login } from "@/api/login";
import Cookies from "js-cookie";
import crypto from "crypto";
import md5 from "js-md5";
import { log } from "util";
import { setToken } from '@/utils/auth'
export default {
  name: "Login",
  data() {
    return {
      // 表单参数
      form: {
        ehuPhone:'',
        ehuPwd:''
      },
      // loginRules: {
      //   ehuPhone: [
      //     { required: true, trigger: "blur", message: "用户名不能为空" }
      //   ],
      //   ehuPwd: [{ required: true, trigger: "blur", message: "密码不能为空" }]
      // },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCookie();
  },
  getmd5: function (pwd) {
    var md5 = crypto.createHash("md5");
    md5.update(pwd);
    var password = md5.digest("hex");
    return password;
  },
  methods: {
    getCookie() {
      const username = Cookies.get("ehuPhone");
      const ehuGridId = Cookies.get("ehuGridId");
      const ehuId = Cookies.get("ehuId");
      const nickname = Cookies.get("nickname");
      const avatarUrl = Cookies.set("avatarUrl");
    },
    /** 提交按钮 */
    handleLogin: function () {
      // setToken('564646')
      // console.log('this.redirect',this.redirect)
      // this.$router.push({ path: this.redirect || "/" });

      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.ehuPwd = md5(this.form.ehuPwd);
          this.form.type = 3;
          this.$store.dispatch("Login", this.form)
            .then((res) => {
              console.log('登录成功',res.data)
              if(res.code == 200 && res.data.ehuType=='我是企业'){
                setToken(res.data.token)
                this.$store.commit('SET_TOKEN', res.data.token);
                this.$store.commit('SET_NAME', res.data.nickname);
                this.$store.commit('SET_AVATAR', res.data.avatarUrl);
                Cookies.set("ehuPhone", res.data.ehuPhone, { expires: 30 });
              Cookies.set("ehuGridId", res.data.ehuGridId, {
                expires: 30,
              });
              Cookies.set("ehuId", res.data.ehuId, { expires: 30 });
              Cookies.set("nickname", res.data.nickname, { expires: 30 });
              Cookies.set("avatarUrl", res.data.avatarUrl, {
                expires: 30,
              });
              this.$router.push({ path: this.redirect || "/" });
              } else {
                this.msgError(res.msg);
            }
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
}
</style>
